package tushu;

import java.io.BufferedReader;
import java.io.FilterReader;
import java.io.InputStreamReader;
import java.io.PipedReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;

import org.junit.jupiter.api.Test;

import tushu.Bean.BookBean;
import tushu.Bean.BookRentJiLuBean;
import tushu.Bean.RentCardBean;
import tushu.dao.rentCardDao;
import tushu.dao.rentJiLuDao;
import tushu.dao.tushuDao;

public class tushu {

	public static void main(String[] args) {
		while (true) {
			System.out.println("1.进书");
			System.out.println("2.借书卡办理");
			System.out.println("3.借书");
			System.out.println("4.还书");
			System.out.println("5.查询某书借出记录");
			System.out.println("6.查询借书卡的借出记录");
			System.out.println("7.显示图书列表(按借出次数排序)");
			tushu t = new tushu();
			t.caozuo();
		}
	}

	public String input() {
		Scanner sc = new Scanner(System.in);
		return sc.nextLine();
	}

	public void caozuo() {
		Scanner sc = new Scanner(System.in);
		String xuanze = sc.nextLine();
		switch (xuanze) {
		case "1":
			jinshu();
			break;
		case "2":
			cardbanli();
			break;
		case "3":
			jieshu();
			break;
		case "4":
			huanshu();
			break;
		case "5":
			jiluByBook();
			break;
		case "6":
			jiluByCard();
			break;
		case "7":
			listBook();
			break;

		default:
			break;
		}
	}

	@Test
	public void jinshu() {
		BookBean book = new BookBean();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入进书图书id");
		int id = Integer.parseInt(sc.nextLine());
		System.out.println("请输入进书图书名称");
		String name = sc.nextLine();
		System.out.println("请输入进书图书数量");
		int number = Integer.parseInt(sc.nextLine());
		System.out.println("请输入进书图书放置区域");
		String address = sc.nextLine();
		book.setId(id);
		book.setBookName(name);
		book.setNumber(number);
		book.setAddress(address);

		tushuDao tushudao = new tushuDao();
		tushudao.insert(book);
	}

	/**
	 * 
	 */
	@Test
	public void cardbanli() {

		RentCardBean card = new RentCardBean();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的卡号");
		int cardId = Integer.parseInt(sc.nextLine());
		System.out.println("请输入你的姓名");
		String name = sc.nextLine();
		// 将借书卡的信息保存在集合
		card.setId(cardId);
		card.setStudentName(name);

		rentCardDao rentDao = new rentCardDao();
		rentDao.insert(card);

//		System.out.println(cardlist.get(0));
	}

	@Test
	public void jieshu() {
		// 一个要改借书卡信息，一个改图书借出次数信息
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的借书卡号");
		Integer cardId = Integer.parseInt(sc.nextLine());
		System.out.println("请输入你借的书名");
		String bookName = sc.nextLine();
		// 获取本地时间
		Date nowtime = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = fmt.format(nowtime);

		BookBean book = new BookBean();
		book.setBookName(bookName);

		tushuDao tushudao = new tushuDao();
		BookBean b = tushudao.selectByName(book);
		// 存在借书卡号，传书名，返回对象，判断数量，修改数量
		int flag = 0;
		if (b.getNumber() > 0) {
			flag = 1;
		} else {
			flag = 0;
		}

		if (flag == 1) {
			System.out.println("借书成功");
		} else {
			System.out.println("图书不存在或库存不足");
			return;
		}

		// 借书卡dao
		RentCardBean rr = new RentCardBean();
		rr.setId(cardId);
		rentCardDao rentdao = new rentCardDao();
		RentCardBean r = rentdao.select(rr);

		// 记录dao
		rentJiLuDao jldao = new rentJiLuDao();
		if (r.getId() != 0) {
			// 这里更新某书的借出记录Bean
			// 插入bookrent 查询次数 修改次数
			BookRentJiLuBean bookrent = new BookRentJiLuBean();
			bookrent.setName(bookName);
			bookrent.setCardId(cardId);
			Integer n = bookrent.getRentCiShu();
			if (n == null) {
				n = 0;
			}
			bookrent.setRentCiShu(n + 1);
			bookrent.setRentPerson(r.getStudentName());
			bookrent.setTime(time);
			jldao.insert(bookrent);
		}
		// 下面使用的Integer.valueOf()方法
//			if(Integer.valueOf(cardlist.get(i).getId()).equals(cardId)) {
//			}
//		}

//		for (int i = 0; i < booklist.size(); i++) {
//			BookBean book = booklist.get(i);
		int cishu = b.getRentCiShu();
		int bookNumber = b.getNumber();

		b.setNumber(bookNumber - 1);
		b.setRentCiShu(cishu + 1);

		if (bookNumber > 0) {

			tushudao.update(b);
		}
	}

//	}
	@Test
	public void h() {
		RentCardBean rr = new RentCardBean();
		rr.setId(1);
		rentCardDao rentdao = new rentCardDao();
		RentCardBean r = rentdao.select(rr);
		System.out.println(r);
	}

	@Test
	public void huanshu() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你还书的书名");
		String bookName = sc.nextLine();
		System.out.println("请输入你的借书卡号");
		Integer cardId = Integer.parseInt(sc.nextLine());

		Date time = Calendar.getInstance().getTime();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String fmtTime = fmt.format(time);

		// Bean
		BookRentJiLuBean jilu = new BookRentJiLuBean();
		BookBean book = new BookBean();

		jilu.setName(bookName);
		jilu.setCardId(cardId);

		book.setBookName(bookName);
		// Dao
		rentJiLuDao jiludao = new rentJiLuDao();
		tushuDao tushudao = new tushuDao();
		// 获取当前书状态
		BookBean book1 = tushudao.selectByName(book);
		// 还书操作
		book.setNumber(book1.getNumber() + 1);
		tushudao.updateKuCun(book);

		//
		jilu.setGuiHuanTime(fmtTime);
		int i = jiludao.updateGuiHuan(jilu);
		System.out.println("i=" + i);
		if (i > 0) {
			System.out.println("还书成功");
			return;
		} else {
			System.out.println("还书失败");
			return;
		}
		// 书名 卡号对应 修改 库存量 还书时间
//		for (int i = 0; i < booklist.size(); i++) {
//			BookBean book = booklist.get(i);
//			BookRentJiLuBean rentBean = lookByIdName(bookName, cardId);
//			
//			if (rentBean == null) {
//				return;
//			}
//			if (book.getBookName().equals(bookName)) {
//				book.setNumber(book.getNumber() + 1);
//				rentBean.setGuiHuanTime(fmtTime);
//			}
//		}
	}

	/**
	 * @return
	 */
	@Test
	public void jiluByBook() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入查询某一本书借出记录的书名：");
		String bookName = sc.nextLine();
		BookRentJiLuBean jilubean = new BookRentJiLuBean();
		jilubean.setName(bookName);

		rentJiLuDao jiludao = new rentJiLuDao();
		List<BookRentJiLuBean> list = jiludao.selectByTiaoJian(jilubean);
		for (int i = 0; i < list.size(); i++) {
			BookRentJiLuBean jilu = list.get(i);
			System.out.println(jilu.toString());
		}
//		for (int i = 0; i < bookrentlist.size(); i++) {
//			BookRentJiLuBean bookrent = bookrentlist.get(i);
//			if (bookrent.getName().equals(bookName)) {
//				System.out.println(bookrent.toString());
//			}
		// 全部书的借出记录
//			System.out.println(bookrentlist.get(i).toString());
//		}
	}

	@Test
	public void jiluByCard() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入查询借书卡的借出记录的卡号：");
		Integer cardId = Integer.parseInt(sc.nextLine());

		BookRentJiLuBean cardRent = new BookRentJiLuBean();
		cardRent.setCardId(cardId);

		rentJiLuDao jiludao = new rentJiLuDao();
		List<BookRentJiLuBean> list = jiludao.selectByTiaoJian(cardRent);
		for (int i = 0; i < list.size(); i++) {
			BookRentJiLuBean bean = list.get(i);
			System.out.println(bean.toString());

		}

	}

	@Test
	public void listBook() {
		System.out.println("全部图书列表：");
		tushuDao dao = new tushuDao();
		List<BookBean> list = dao.selectAll();

		Collections.sort(list, new Comparator<BookBean>() {

			@Override
			public int compare(BookBean o1, BookBean o2) {
				return o2.getRentCiShu() - o1.getRentCiShu();
			}

		});
		for (int i = 0; i < list.size(); i++) {
			BookBean book = list.get(i);
			System.out.println(book.toString());
		}

	}
}
